﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace System.Data.DataSetGenerator
{
    /// <summary>
    /// Represents a collection of TableDescriptor objects
    /// </summary>
    public sealed class TableCollection : Collection<TableDescriptor>
    {

        /// <summary>
        /// Creates a new TableDescriptor object by using the  
        /// giving name and adds it to the collection.
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public TableDescriptor Add(String tableName)
        {
            if (String.IsNullOrEmpty(tableName))
                throw new ArgumentNullException("tableName");
            TableDescriptor tableDescriptor = new TableDescriptor() { TableName = tableName };
            Add(tableDescriptor);
            return tableDescriptor;
        }

        /// <summary>
        /// Creates an array of the TableDescriptor elements and adds them to the end of the collection.
        /// </summary>
        /// <param name="tableNames"></param>
        public void AddRange(IEnumerable<String> tableNames)
        {
            foreach (String tableName in tableNames)
            {
                Add(tableName);
            }
        }

        /// <summary>
        /// Gets a specific element of the collection by name. 
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public TableDescriptor this[String tableName]
        {
            get
            {
                if (String.IsNullOrEmpty(tableName))
                    throw new ArgumentNullException("tableName");

                foreach (var tableDescriptor in this)
                {
                    if (tableName == tableDescriptor.TableName)
                        return tableDescriptor;
                }
                return null;
            }
        }

        public bool Contains(String tableName)
        {
            var result = this[tableName];
            return result != null;
        }
    }
}
